#coding=utf-8
#Boa:Dialog:DlgSelectPro

import wx

import PanelProduct

from MyListCtrl import *
from product import *

import kit


def create_temp(parent, type, peerObj=None):
    dlg = DlgSelectPro(parent)
    dlg.setViewState(type, peerObj)
    return dlg

# 以下方法会导致DlgSelectPro关闭时，auiNotebook自动切换到第一个打开DlgSelectPro的标签页
_dlg_sale = None
_dlg_pur = None
def create(parent, type, peerObj=None):
    global _dlg_sale
    global _dlg_pur
    
    if(type == TypeSale):
        if(_dlg_sale is None):
            _dlg_sale = DlgSelectPro(parent)
            _dlg_sale.setViewState(type, peerObj)
        else:
            _dlg_sale.changePeer(peerObj)
            #_dlg_sale.Reparent(parent)
        
        _dlg_sale.reInit()
        
##        _dlg_sale.clearProDetail()
##        _dlg_sale.nbPro.SetSelection(0)    
            
        return _dlg_sale

    elif(type == TypePurchase):   
        if(_dlg_pur is None):
            _dlg_pur = DlgSelectPro(parent)
            _dlg_pur.setViewState(type, peerObj)
        else:
            _dlg_pur.changePeer(peerObj)
    
        _dlg_pur.reInit()
##        _dlg_pur.clearProDetail()
##        _dlg_pur.nbPro.SetSelection(0)
        
        return _dlg_pur

[wxID_DLGSELECTPRO, wxID_DLGSELECTPROBTNABOLISH, wxID_DLGSELECTPROBTNCONFIRM, 
 wxID_DLGSELECTPROBTNDEL, wxID_DLGSELECTPROBTNMOVEDOWN, 
 wxID_DLGSELECTPROBTNMOVEUP, wxID_DLGSELECTPROLCPROONSALE, 
 wxID_DLGSELECTPROTSTAT, 
] = [wx.NewId() for _init_ctrls in range(8)]

[TypeSale, TypePurchase] = [i for i in range(2)]

class DlgSelectPro(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGSELECTPRO, name=u'DlgSelectPro',
              parent=prnt, pos=wx.Point(-2, 119), size=wx.Size(1282, 651),
              style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u6dfb\u52a0\u5546\u54c1-\u91c7\u8d2d\u5546\u54c1')
        self.SetClientSize(wx.Size(1274, 617))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.Center(wx.HORIZONTAL)
        self.Bind(wx.EVT_CLOSE, self.OnDlgSelectProClose)

        self.lcProOnSale = wx.ListCtrl(id=wxID_DLGSELECTPROLCPROONSALE,
              name=u'lcProOnSale', parent=self, pos=wx.Point(728, 56),
              size=wx.Size(488, 248), style=wx.LC_SINGLE_SEL | wx.LC_REPORT)

        self.btnConfirm = wx.Button(id=wxID_DLGSELECTPROBTNCONFIRM,
              label=u'\u786e\u5b9a', name=u'btnConfirm', parent=self,
              pos=wx.Point(728, 360), size=wx.Size(64, 26), style=0)
        self.btnConfirm.Bind(wx.EVT_BUTTON, self.OnBtnConfirmButton)

        self.btnAbolish = wx.Button(id=wxID_DLGSELECTPROBTNABOLISH,
              label=u'\u53d6\u6d88', name=u'btnAbolish', parent=self,
              pos=wx.Point(904, 360), size=wx.Size(64, 26), style=0)
        self.btnAbolish.Bind(wx.EVT_BUTTON, self.OnBtnAbolishButton)

        self.btnDel = wx.Button(id=wxID_DLGSELECTPROBTNDEL,
              label=u'\u5220\u9664', name=u'btnDel', parent=self,
              pos=wx.Point(816, 360), size=wx.Size(64, 26), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton)

        self.tStat = wx.StaticText(id=wxID_DLGSELECTPROTSTAT,
              label=u'\u603b\u6570\u91cf\uff1a\n\u603b\u91d1\u989d\uff1a',
              name=u'tStat', parent=self, pos=wx.Point(1064, 312),
              size=wx.Size(154, 32), style=0)

        self.btnMoveUp = wx.Button(id=wxID_DLGSELECTPROBTNMOVEUP,
              label=u'\u2227', name=u'btnMoveUp', parent=self,
              pos=wx.Point(1224, 136), size=wx.Size(24, 40), style=0)
        self.btnMoveUp.Bind(wx.EVT_BUTTON, self.OnBtnMoveUpButton,
              id=wxID_DLGSELECTPROBTNMOVEUP)

        self.btnMoveDown = wx.Button(id=wxID_DLGSELECTPROBTNMOVEDOWN,
              label=u'\u2228', name=u'btnMoveDown', parent=self,
              pos=wx.Point(1224, 192), size=wx.Size(24, 40), style=0)
        self.btnMoveDown.Bind(wx.EVT_BUTTON, self.OnBtnMoveDownButton,
              id=wxID_DLGSELECTPROBTNMOVEDOWN)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.myInitNoteBook()
        self.myInitLC()
    
    def reInit(self):
        self.clearProDetail()
        self.nbPro.SetSelection(0)
        # 重置keyword、category选项
        print u'TODO - 重置keyword、category选项(会不会影响效率)'

    def setViewState(self, type, peerObj):
        if(type == TypeSale):            
            self.panel = PanelProduct.PanelProduct(self.nbPro, 
                                    PanelProduct.TypeSale, peerObj)
            self.nbPro.AddPage(imageId=-1, page=self.panel, select=False,
                  text=u'\u9500\u552e\u5386\u53f2')
            self.panelPro.setViewTypeProAll(PanelProduct.TypeSale)
            
        else:
            self.panel = PanelProduct.PanelProduct(self.nbPro, 
                                    PanelProduct.TypePurchase, peerObj)
            self.nbPro.AddPage(imageId=-1, page=self.panel, select=False,
                  text=u'\u91c7\u8d2d\u5386\u53f2')
            self.panelPro.setViewTypeProAll(PanelProduct.TypePurchase)  
    
    def changePeer(self, peerObj):
        #if(peerObj.getValue(peerObj.COL_ID) != dlg.)
        if(peerObj is None):
            self.panel.clearSelectPeer()
        else:
            self.panel.setSelectPeer(peerObj)
        
    def myInitNoteBook(self):
        self.nbPro = wx.Notebook(id=-1, name=u'nbPro',
              parent=self, pos=wx.Point(10, 10), size=wx.Size(620, 600),
              style=0)
        
        self.panelPro = PanelProduct.PanelProduct(self.nbPro, PanelProduct.TypeProAll)
        self.nbPro.AddPage(imageId=-1, page=self.panelPro, select=True,
              text=u'\u5546\u54c1\u5217\u8868')

        
    def myInitLC(self):
        cols = [(u'编号', wx.LIST_FORMAT_LEFT, 0),
                (u'名称', wx.LIST_FORMAT_LEFT, 100),
                (u'规格',  wx.LIST_FORMAT_LEFT, 100), 
                (u'单位',  wx.LIST_FORMAT_LEFT, 50), 
                (u'数量',  wx.LIST_FORMAT_RIGHT, 70), 
                (u'单价',  wx.LIST_FORMAT_RIGHT, 70),
                (u'金额', wx.LIST_FORMAT_RIGHT, 80),
                (u'备注', wx.LIST_FORMAT_RIGHT, 100)]
        
        self.iCnt = 4
        self.iAmount = 6        
        for i in range(len(cols)):            
            self.lcProOnSale.InsertColumn(i, cols[i][0], cols[i][1], cols[i][2])
    
    def refreshStat(self):
        cnt = 0.0
        amount = 0.0
        for row in range(self.lcProOnSale.GetItemCount()):
            cnt += float(self.lcProOnSale.GetItem(row, self.iCnt).GetText())
            amount += float(self.lcProOnSale.GetItem(row, self.iAmount).GetText())
            
        text = u'总数量：%g\n总金额：%g' %(cnt, amount)
        self.tStat.SetLabel(text)
        
    def clearProDetail(self):
        self.lcProOnSale.DeleteAllItems()
        
    def appendProDetail(self, dataList):
        '''dataList - [[proId,proName,proSpec,unit,cnt,price,amount],]'''
        #data = [proId,proName,unit,cnt,price,amount]
        
        for i in range(len(dataList)):
            row = self.lcProOnSale.InsertStringItem(sys.maxint, '')
            for j in range(len(dataList[0])):
                self.lcProOnSale.SetStringItem(row, j, kit.myStr(dataList[i][j]))

        self.refreshStat()
        
    def getProSaleList(self):
        '''return - [[proId,proName,unit,cnt,price,amount],]'''
        return self.dataList
    
        
    def OnBtnConfirmButton(self, event):
        self.dataList = []
        for row in range(self.lcProOnSale.GetItemCount()):
            dataRow = []
            for col in range(self.lcProOnSale.GetColumnCount()):
                dataRow.append(self.lcProOnSale.GetItem(row, col).GetText())
            
            self.dataList.append(dataRow)
            
        self.EndModal(wx.ID_OK)
        print 'OnBtnConfirmButton'

    def OnBtnAbolishButton(self, event):
        #print('OnBtnAbolishButton ', self.GetParent().GetSelection())
        dlg = wx.MessageDialog(self, u'取消修改', u'提示',wx.YES_NO|wx.ICON_QUESTION)
        if(dlg.ShowModal() != wx.ID_YES):
            return
            
        self.EndModal(wx.ID_CANCEL)
        #print(self.GetParent().GetSelection())
        #event.Skip()

    def OnBtnDelButton(self, event):
        item = self.lcProOnSale.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        self.lcProOnSale.DeleteItem(item)
        self.refreshStat()

    def OnDlgSelectProClose(self, event):
        self.EndModal(wx.ID_CANCEL)
        #event.Skip()

    def swap(self, lc, item1, item2):
        for col in range(lc.GetColumnCount()):
            temp = lc.GetItem(item1, col).GetText()
            lc.SetStringItem(item1, col, lc.GetItem(item2, col).GetText())
            lc.SetStringItem(item2, col, temp)
        
    def OnBtnMoveDownButton(self, event):
        item = self.lcProOnSale.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1 or item == self.lcProOnSale.GetItemCount()-1):
            return
        
        self.swap(self.lcProOnSale, item, item+1)
        #self.lcProOnSale.SetItemState(item+1, 0, wx.LIST_STATE_SELECTED)
        self.lcProOnSale.Select(item+1)
        #self.lcProOnSale.Deselect(item)
        self.lcProOnSale.SetFocus()


    def OnBtnMoveUpButton(self, event):
        item = self.lcProOnSale.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1 or item == 0):
            return
        
        self.swap(self.lcProOnSale, item, item-1)
        #self.lcProOnSale.SetItemState(item-1, 0, wx.LIST_STATE_SELECTED)
        self.lcProOnSale.Select(item-1)
        #self.lcProOnSale.Deselect(item)
        self.lcProOnSale.SetFocus()

              
